package com.example.final_project.Usr.frag;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.R;
import com.example.final_project.Usr.Activity.AppSetActivity;
import com.example.final_project.Usr.Activity.AuthorActivity;
import com.example.final_project.Usr.Activity.LoginActivity;
import com.example.final_project.Usr.Activity.MyCarActivity;
import com.example.final_project.Usr.Activity.ReleaseActivity;
import com.example.final_project.Usr.adapter.ShopAdapter;
import com.example.final_project.Usr.entry.UserStore;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

public class UsrFragment extends Fragment {

    private LinearLayout login_liner,mycar,author,work;
    private Context context;
    private View view;
    private ImageView user_head,app_set;
    private TextView user_name;
    private RecyclerView store;
    private Button release;
    public static final int LOADING=0;
    private String name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_usr, container, false);
        findByID();
        RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(context));
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
        getData();
        //设置登录点击事件
        login_liner = view.findViewById(R.id.unlogin_LL);
        login_liner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent login_int = new Intent(getActivity(), LoginActivity.class);
                startActivity(login_int);
            }
        });
        //设置 设置 点击事件
        app_set = view.findViewById(R.id.app_set);
        app_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AppSetActivity.class);
                startActivity(intent);
            }
        });

        //判断用户是否登录
        isLogin();

        //设置爱车点击事件
        mycar = view.findViewById(R.id.user_mycar);
        mycar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCarActivity.class);
                startActivity(intent);
            }
        });

        //创作者中心点击事件
        author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AuthorActivity.class);
                context.startActivity(intent);
            }
        });

        //发布点击事件
        release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ReleaseActivity.class);
                intent.putExtra("name",name);
                context.startActivity(intent);
            }
        });

        return view;


    }

    public void findByID(){
        context = getContext();
        user_head = view.findViewById(R.id.user_head);
        user_name = view.findViewById(R.id.user_name);
        store = view.findViewById(R.id.shops_position);
        author = view.findViewById(R.id.user_author);
        release = view.findViewById(R.id.user_release);
        work = view.findViewById(R.id.user_author_work);
    }

    public void isLogin(){
        LinearLayout login_LL = view.findViewById(R.id.login_LL);
        LinearLayout unlogin_LL = view.findViewById(R.id.unlogin_LL);
        SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        if(sp != null && !sp.getString("name","").equals("")){
            login_LL.setVisibility(View.VISIBLE);
            unlogin_LL.setVisibility(View.GONE);

            if(sp.getString("sex","").equals("男")){
                user_head.setImageResource(R.mipmap.boy_head);
            }else{
                user_head.setImageResource(R.mipmap.girl_head);
            }
            name = sp.getString("name","");
            user_name.setText(name);
        }else{
            login_LL.setVisibility(View.GONE);
            unlogin_LL.setVisibility(View.VISIBLE);
        }
    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case LOADING:
                    List<UserStore> shops = (List<UserStore>)msg.obj;
                    ShopAdapter shopAdapter = new ShopAdapter(context,shops);
                    store.setAdapter(shopAdapter);
                    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
                    store.setLayoutManager(staggeredGridLayoutManager);
                    break;
            }

        }
    };

    //获取商品数据
    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/getstore")
                .bind(context)
                .setOnResList(UserStore.class,shops ->{
                    Message message = Message.obtain();
                    message.what=LOADING;
                    message.obj=shops;
                    handler.sendMessage(message);
                })
                .get();
    }

}